package com.yycx.module.bus.provider.handler.haiying.deptpay;

import com.yycx.bpm.provider.listener.FlowPageInterceptor;
import com.yycx.bpm.provider.model.TaskResponse;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.admin.provider.service.BaseUserService;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.*;
import org.flowable.common.engine.api.query.Query;
import org.flowable.engine.RuntimeService;
import org.flowable.task.api.TaskInfoQuery;
import org.flowable.variable.api.persistence.entity.VariableInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 部门计算资金支付审批任务列表
 *
 * @author zyf
 */
@Component
public class DeptPayPaymentTaskTodoListHandler implements FlowPageInterceptor<TaskResponse> {

    @Autowired
    private BusPaymentService paymentService;


    @Autowired
    private BusDeptService deptService;


    @Autowired
    private BaseUserService userService;

    @Autowired
    private BusSalemanService salemanService;

    @Autowired
    private BusInvoiceunitService invoiceunitService;

    @Autowired
    private BusSalemanBankService salemanBankService;


    @Autowired
    protected RuntimeService runtimeService;

    @Override
    public void prepare(Query query, EntityMap params) {
        TaskInfoQuery taskQuery = (TaskInfoQuery) query;
        String companyId = OpenHelper.getCompanyId().toString();
        String paymentNo = params.get("paymentNo");
        String deptId = params.get("deptId");
        String supplierId = params.get("supplierId");
        String customId = params.get("customId");
        String amountType = params.get("amountType");
        String taskName = params.get("taskName");
        String linkMan = params.get("linkMan");
        BusPayment payment = paymentService.getByProperty("paymentNo", paymentNo);
        if (FlymeUtils.isNotEmpty(payment)) {
            taskQuery.processInstanceBusinessKey(payment.getPaymentId().toString());
        } else {
            if (FlymeUtils.isNotEmpty(paymentNo)) {
                taskQuery.processVariableValueLike("paymentNo", '%' + paymentNo);
            }
        }
        if (FlymeUtils.isNotEmpty(amountType)) {
            taskQuery.processVariableValueEquals("amountType", amountType);
        }
        if (FlymeUtils.isNotEmpty(deptId)) {
            taskQuery.processVariableValueEquals("deptId", Long.parseLong(deptId));
        }
        if (FlymeUtils.isNotEmpty(linkMan)) {
            taskQuery.processVariableValueLike("linkMan", linkMan);
        }
        if (FlymeUtils.isNotEmpty(supplierId)) {
            taskQuery.processVariableValueEquals("supplierId", Long.parseLong(supplierId));
        }
        if (FlymeUtils.isNotEmpty(customId)) {
            taskQuery.processVariableValueEquals("customId", Long.parseLong(customId));
        }
        if (FlymeUtils.isNotEmpty(taskName)) {
            taskQuery.taskName(taskName);
        }
        taskQuery.taskTenantId("flp");
        taskQuery.processDefinitionKey("process_dept_amount_pay_flp");
    }

    @Override
    public List<EntityMap> complete(Query query, List<TaskResponse> data, EntityMap params) {
        List<EntityMap> list = new ArrayList<>();
        if (FlymeUtils.isNotEmpty(data)) {
            for (TaskResponse taskResponse : data) {
                EntityMap map = new EntityMap();
                map.put("processInstanceId", taskResponse.getProcessInstanceId());
                String paymentId = taskResponse.getBusinessKey();
                if (FlymeUtils.isNotEmpty(paymentId)) {
                    BusPayment payment = paymentService.getById(paymentId);
                    if (FlymeUtils.isNotEmpty(payment)) {
                        Long userId = payment.getUserId();
                        Long deptId = payment.getDeptId();
                        if (FlymeUtils.isNotEmpty(deptId)) {
                            BusDept dept = deptService.getById(deptId);
                            map.put("deptName", dept.getDeptName());
                            map.put("saleManId", dept.getSalemanId());
                        }
                        //单据类型
                        String amountType = payment.getAmountType();
                        BaseUser user = userService.getById(userId);
                        //结算单
                        if (AmountTypeEnum.Invoice.getCode().equals(amountType)) {
                            getSettleMap(payment, map);
                        }
                        map.put("paymentNo", payment.getPaymentNo());
                        map.put("paymentId", payment.getPaymentId());
                        map.put("amountType", amountType);
                        map.put("amount", payment.getAmount());
                        map.put("invoiceAmount", payment.getInvoiceAmount());
                        if (FlymeUtils.isNotEmpty(user)) {
                            map.put("nickName", user.getNickName());
                        }
                        map.put("createTime", payment.getCreateTime());
                        map.put("amountDate", DateUtils.formatDate(payment.getAmountDate(), "yyyy-MM-dd"));
                        map.put("paymentState", payment.getPaymentState());
                        map.put("assignee", taskResponse.getAssignee());

                        map.put("taskName", taskResponse.getName());
                        map.put("taskId", taskResponse.getId());
                        map.put("endTime", taskResponse.getEndTime());
                        map.put("orderId", payment.getOrderId());

                    }
                    list.add(map);
                }
            }
        }
        return list;
    }

    /**
     * 查询结算单信息
     *
     * @param payment
     * @param map
     */
    private void getSettleMap(BusPayment payment, EntityMap map) {
        Long invoiceUnitId = payment.getInvoiceUnitId();
        BusInvoiceunit invoiceUnit=invoiceunitService.getById(invoiceUnitId);
        if (FlymeUtils.isNotEmpty(invoiceUnit)) {
            map.put("unitName", invoiceUnit.getInvoiceunitName());
        }
    }
}
